package com.framework.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.framework.core.log.annotation.AutoLog;
import com.framework.core.result.R;
import com.framework.system.entity.dto.SysDictItemAddDTO;
import com.framework.system.entity.dto.SysDictItemQueryDTO;
import com.framework.system.entity.dto.SysDictItemUpdateDTO;
import com.framework.system.entity.vo.SysDictItemListVO;
import com.framework.system.entity.vo.SysDictItemVO;
import com.framework.system.service.ISysDictItemService;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;

/**
 * 字典项管理
 *
 * @author Kevin
 * @since 2021-06-10
 */
@RestController
@RequestMapping("/system/sysDictItem")
@AllArgsConstructor
public class SysDictItemController {

    private ISysDictItemService sysDictItemService;

    /**
     * 分页查询字典项列表
     */
    @GetMapping("/selectSysDictItemWithPage")
    @SaCheckPermission("system:sysDictItem:selectSysDictItemWithPage")
    @AutoLog(module = "字典项管理", title = "分页查询字典项列表")
    public R<Page<SysDictItemListVO>> selectSysDictItemWithPage(Page reqPage, SysDictItemQueryDTO req) {
        return R.success(sysDictItemService.selectSysDictItemWithPage(reqPage, req));
    }

    /**
     * 新增字典项
     */
    @PostMapping("/addSysDictItem")
    @SaCheckPermission("system:sysDictItem:addSysDictItem")
    @AutoLog(module = "字典项管理", title = "新增字典项")
    public R<Void> addSysDictItem(@Validated @RequestBody SysDictItemAddDTO req) {
        sysDictItemService.addSysDictItem(req);
        return R.success();
    }

    /**
     * 修改字典项
     */
    @PostMapping("/updateSysDictItem")
    @SaCheckPermission("system:sysDictItem:updateSysDictItem")
    @AutoLog(module = "字典项管理", title = "修改字典项")
    public R<Void> updateSysDictItem(@Validated @RequestBody SysDictItemUpdateDTO req) {
        sysDictItemService.updateSysDictItem(req);
        return R.success();
    }

    /**
     * 删除字典项
     */
    @PostMapping("/removeSysDictItem")
    @SaCheckPermission("system:sysDictItem:removeSysDictItem")
    @AutoLog(module = "字典项管理", title = "删除字典项")
    public R<Void> removeSysDictItem(@RequestParam String ids) {
        sysDictItemService.removeSysDictItem(ids);
        return R.success();
    }

    /**
     * 查看字典项
     */
    @GetMapping("/getSysDictItem")
    @SaCheckPermission("system:sysDictItem:getSysDictItem")
    @AutoLog(module = "字典项管理", title = "查看字典项")
    public R<SysDictItemVO> getSysDictItem(@RequestParam String id) {
        return R.success(sysDictItemService.getSysDictItem(id));
    }

    /**
     * 导出字典项
     *
     * @param req
     * @param response
     */
    @GetMapping("/exportSysDictItem")
    @SaCheckPermission("system:sysDictItem:exportSysDictItem")
    @AutoLog(module = "字典项管理", title = "导出字典项")
    @SneakyThrows
    public void exportSysDictItem(SysDictItemQueryDTO req, HttpServletResponse response) {
        String filename = "字典项列表" + "_" + System.currentTimeMillis() + ".xlsx";
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
        EasyExcel.write(response.getOutputStream())
                .head(SysDictItemListVO.class)
                .sheet("字典项列表")
                .doWrite(sysDictItemService.selectSysDictItemWithList(req));
    }

}

